<?php
/**
 *Router
 *
 *@author S.Feil, T.Ziegler, M.El-Hindi <goatymail@googlemail.com>
 *27 May 2012
 *@copyright Copyright (c) 2012, S.Feil, T.Ziegler, M.El-Hindi
 *@package cms
 *
 */
/**
 * Class Router
 * handel request and return instance of controller
 *
 *@package lib
 */

//the loader loads and instanciates controllers
include_once(CMS_ROOT.'content/lib/cms_loader.class.php');

/**
 *the class router analyzes the requests and instanciates the right controller
 */
class Router{

    /**
     * takes the GET-parameters and calls the controller
     * @param Array $aRequest Parameters that specify the controller usually GET-parameters
     */
    public static function route($aRequest){
        //get first element of GET-parameters
        $sFirstElement= array_shift($aRequest);

        //check if request is a ajax request
        //ture if first parameter is "ajax"
        if($sFirstElement=="ajax"){
            //load controller specified in key "co" of GET-parameters
            $oCInstance = Loader::loadController($aRequest['co']);

            //directly call a method on the controller
            //the method is specifeid in the "m" key of GET-parameters
            if (method_exists($oCInstance,$aRequest['m'])){
                $oCInstance->$aRequest['m']($_POST);
            }

            //no ajax request
        }else{

            //load controller and pass $aRequest as arguments of execute mehtod
            $oCInstance = Loader::loadController($sFirstElement);
            $oCInstance->execute($aRequest);
        }
    }

    /**
     * Called every time a not yet included class is used
     * @param string $sClassName class to be loaded
     */
    function __autoload($sClassName){
        Loader::loadClass($sClassName);
    }
}

?>